﻿@inject IStringLocalizer<TreeViews> Localizer

<div class="row form-inline">
    <div class="col-12 col-lg-auto">
        <RadioList IsButton="true" @bind-Value="@IsReset" Items="@ResetItems" Color="Color.Success" ShowLabel="true" DisplayText="@Localizer["TreeViewNormalRadioListDisplayText"]"></RadioList>
    </div>
    <div class="col-12 col-lg-auto">
        <Checkbox DisplayText="@Localizer["TreeViewCheckboxCheckBoxDisplayText1"]" ShowAfterLabel="true" @bind-Value="@AutoCheckChildren" />
        <Checkbox DisplayText="@Localizer["TreeViewCheckboxCheckBoxDisplayText2"]" ShowAfterLabel="true" @bind-Value="@AutoCheckParent" class="ms-3" />
    </div>
    <div class="col-12 col-lg-auto">
        <Button Text="@Localizer["TreeViewCheckboxButtonText"]" OnClick="OnRefresh" />
    </div>
</div>
<TreeView TItem="TreeFoo" Items="@CheckedItems" ShowCheckbox="true" OnTreeItemChecked="@OnTreeItemChecked" IsReset="@IsReset" AutoCheckChildren="@AutoCheckChildren" AutoCheckParent="@AutoCheckParent" />
<ConsoleLogger @ref="Logger" class="mt-3" />

@code {
    [NotNull]
    private ConsoleLogger? Logger { get; set; }

    private bool AutoCheckChildren { get; set; }

    private bool AutoCheckParent { get; set; }

    private void OnRefresh()
    {
        CheckedItems = GetCheckedItems();
    }

    private List<TreeViewItem<TreeFoo>> CheckedItems { get; set; } = GetCheckedItems();

    private static List<TreeViewItem<TreeFoo>> GetCheckedItems()
    {
        var ret = TreeFoo.GetTreeItems();
        ret[1].IsActive = true;
        ret[1].Items[1].CheckedState = CheckboxState.Checked;
        return ret;
    }

    private bool IsReset { get; set; }

    private List<SelectedItem> ResetItems { get; } = new List<SelectedItem>()
    {
        new("True", "Reset"),
        new("False", "Keep")
    };

    private Task OnTreeItemChecked(List<TreeViewItem<TreeFoo>> items)
    {
        Logger.Log($"当前共选中{items.Count}项");
        return Task.CompletedTask;
    }
}
